﻿using NDK.Module.BLL;
using NDK.Module.Model;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace NDK.Module.StationLogic
{
    public class DataProcess
    {
        public DataProcess()
        {
            Thread thread1 = new Thread(() => DataProcess1())
            {
                IsBackground = true
            };
            thread1.Start();

            Thread thread2 = new Thread(() => DataProcess2())
            {
                IsBackground = true
            };
            thread2.Start();
        }

        public void DataProcess1()
        {
            while (true)
            {
                try
                {
                    if (MeasureStation.Instance.DataQueue1 != null)
                    {
                        if (MeasureStation.Instance.DataQueue1.Count > 0)
                        {
                            LoadingMove.Instance.DataQueue1.TryDequeue(out MeasureProduct MP);
                            PPG.Instance.DataQueue1.TryDequeue(out MeasureProduct MP1);
                            Transform.Instance.DataQueue1.TryDequeue(out MeasureProduct MP2);
                            MeasureStation.Instance.DataQueue1.TryDequeue(out MeasureProduct MP3);
                            MP.MeasureData.AddRange(MP1.MeasureData);
                            MP.MeasureData.AddRange(MP2.MeasureData);
                            MP.MeasureData.AddRange(MP3.MeasureData);
                            var re = ProcessData.PrcessData(MP);
                            UnloadFlip.Instance.ResultQueue1.Enqueue(re);

                            string childPath = DateTime.Now.ToString("yyyy-MM-dd") + "\\" + Common.CurProType;
                            string Fix1SaveImgSN = MP.SN;
                            if (Common.GRR循环模式)
                                Fix1SaveImgSN += $"_{Common.CurGRR循环次数}_Fix1_{DateTime.Now.ToString("HHmmss")}";
                            else
                                Fix1SaveImgSN += $"_Fix1" + DateTime.Now.ToString("yyyyMMddHHmmss_1");
                            string Fix1LocalPath = Common.picSavePath + "\\" + childPath + "\\" + Fix1SaveImgSN;

                            Task.Run(() =>
                            {
                                var retList_Fix1 = new List<bool>();
                                //下激光
                                retList_Fix1.Add(PublicMethod.SaveImageLaser(MP2.DownLaser1, Fix1LocalPath, "下激光"));

                                //侧激光群组
                                retList_Fix1.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_长边1, Fix1LocalPath, "长边1"));
                                retList_Fix1.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_长边2, Fix1LocalPath, "长边2"));
                                retList_Fix1.Add(PublicMethod.SaveImageLaser(MP3.UpLaser, Fix1LocalPath, "上激光"));
                                retList_Fix1.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_短边1, Fix1LocalPath, "短边1"));
                                retList_Fix1.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_短边2, Fix1LocalPath, "短边2"));
                                //相机
                                retList_Fix1.Add(PublicMethod.SaveImageWithBarCode(MP3.UpCamImage_1, Fix1LocalPath, "上CCD1"));
                                retList_Fix1.Add(PublicMethod.SaveImageWithBarCode(MP3.UpCamImage_2, Fix1LocalPath, "上CCD2"));
                                retList_Fix1.Add(PublicMethod.SaveImageWithBarCode(MP3.UpCamImage_3, Fix1LocalPath, "上CCD3"));
                            });
                        }
                    }
                }
                catch (System.Exception)
                {

                }
                Thread.Sleep(100);
            }
        }

        public void DataProcess2()
        {
            while (true)
            {
                try
                {
                    if (MeasureStation.Instance.DataQueue2 != null)
                    {
                        if (MeasureStation.Instance.DataQueue2.Count > 0)
                        {
                            LoadingMove.Instance.DataQueue2.TryDequeue(out MeasureProduct MP);
                            PPG.Instance.DataQueue2.TryDequeue(out MeasureProduct MP1);
                            Transform.Instance.DataQueue2.TryDequeue(out MeasureProduct MP2);
                            MeasureStation.Instance.DataQueue2.TryDequeue(out MeasureProduct MP3);
                            MP.MeasureData.AddRange(MP1.MeasureData);
                            MP.MeasureData.AddRange(MP2.MeasureData);
                            MP.MeasureData.AddRange(MP3.MeasureData);
                            var re = ProcessData.PrcessData(MP);
                            UnloadFlip.Instance.ResultQueue2.Enqueue(re);

                            string childPath = DateTime.Now.ToString("yyyy-MM-dd") + "\\" + Common.CurProType;
                            string Fix2SaveImgSN = MP.SN;
                            if (Common.GRR循环模式)
                                Fix2SaveImgSN += $"_{Common.CurGRR循环次数}_Fix2_{DateTime.Now.ToString("HHmmss")}";
                            else
                                Fix2SaveImgSN += $"_Fix2" + DateTime.Now.ToString("yyyyMMddHHmmss_2");
                            string Fix2LocalPath = Common.picSavePath + "\\" + childPath + "\\" + Fix2SaveImgSN;

                            Task.Run(() =>
                            {
                                var retList_Fix2 = new List<bool>();
                                //下激光
                                retList_Fix2.Add(PublicMethod.SaveImageLaser(MP2.DownLaser1, Fix2LocalPath, "下激光"));

                                //侧激光群组
                                retList_Fix2.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_长边1, Fix2LocalPath, "长边1"));
                                retList_Fix2.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_长边2, Fix2LocalPath, "长边2"));
                                retList_Fix2.Add(PublicMethod.SaveImageLaser(MP3.UpLaser, Fix2LocalPath, "上激光"));
                                retList_Fix2.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_短边1, Fix2LocalPath, "短边1"));
                                retList_Fix2.Add(PublicMethod.SaveImageLaser(MP3.SideLaser_短边2, Fix2LocalPath, "短边2"));

                                retList_Fix2.Add(PublicMethod.SaveImageWithBarCode(MP3.UpCamImage_1, Fix2LocalPath, "上CCD1"));
                                retList_Fix2.Add(PublicMethod.SaveImageWithBarCode(MP3.UpCamImage_2, Fix2LocalPath, "上CCD2"));
                                retList_Fix2.Add(PublicMethod.SaveImageWithBarCode(MP3.UpCamImage_3, Fix2LocalPath, "上CCD3"));
                            });
                        }
                    }
                }
                catch (System.Exception)
                {

                }
                Thread.Sleep(100);
            }
        }
    }
}
